//
//  ShoppingMallViewController.m
//  firstpoint
//
//  Created by 泽中 on 2017/6/28.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "ShoppingMallViewController.h"
#import "LeeShoppingTableViewCell.h"//商品cell
#import "LeeGoodsListViewController.h"//商品列表
#import "ShoppingFenLeiViewController.h"
#import "ShoppingCartViewController.h"//购物车
#import "CommodityDetailsViewController.h"
#import "LeeMingPingGengDuoViewController.h"//名品跟多
#import "PSCarouselView.h"

@interface ShoppingMallViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UITextFieldDelegate,UICollectionViewDelegate,PSCarouselDelegate>


{
    NSArray*imgArr;//轮播图数组
    PSCarouselView*showView;
}
@property(nonatomic,strong)UITextField*SSSRTextField;//输入框
@property(nonatomic,strong)UIView *ShoppingBigView;//表头View
@property(nonatomic,strong)UITableView *ShoppingTabelView;//表
@property (nonatomic, strong) UIPageControl *pageCtrl;
@property (nonatomic, strong) UIView *CJFView;
@property (nonatomic,strong)NSArray *sectionTitleArr;
@property (nonatomic,strong)NSArray *shoppingArray;//名品商品
@property (nonatomic,strong)NSArray *likeshoppingArray;//名品商品
@property ( nonatomic) UIPageControl *pageControl;
@end

@implementation ShoppingMallViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [showView startMoving];

    self.navigationController.navigationBar.hidden=NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [showView stopMoving];
}
- (IBAction)didDragSpeedSlider:(UISlider *)sender
{
    showView.movingTimeInterval = sender.value;
    [showView stopMoving];
    [showView startMoving];
}
- (void)viewDidLoad {
    _sectionTitleArr = @[@[@"名品抢购",@"猜你喜欢"],@[@"更多>>",@"换一批"]];
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    [self setNavButton];//导航条
    self.title=@"元点商城";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addsousuo];//搜索框
    [self addShoppingMallTableView];//添加表
    _shoppingArray = [NSArray array];
    [self scrollHttp];//轮播图
    [self httpRquestURLshopping];//
}
-(void)setNavButton
{
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,25)];
    [rightButton setImage:[UIImage imageNamed:@"ydsctbb"]forState:UIControlStateNormal];
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [rightButton addTarget:self action:@selector(rightButtonClick)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem= rightItem;
    UIButton*leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,0,25,25)];
    [leftBtn setImage:[UIImage imageNamed:@"dbscs"]forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(gouwuCheBtnClcike)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*leftItme = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    leftBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.navigationItem.leftBarButtonItem= leftItme;
    
}
-(void)gouwuCheBtnClcike{
    ShoppingCartViewController*vc=[ShoppingCartViewController new];
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)rightButtonClick
{
    ShoppingFenLeiViewController*vc=[ShoppingFenLeiViewController new];
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}
//搜索框
-(void)addsousuo{
    UIView*NVView =[[UIView alloc]init];
    NVView.frame=CGRectMake(0, 64, width_screen, 40);
    NVView.backgroundColor = COLOR(212, 217, 220, 1);
    [self.view addSubview:NVView];
    //输入框背景
    UIView * sosuobgView = [[UIView alloc]initWithFrame:CGRectMake(10, 5, width_screen-20 , 30)];
    sosuobgView.backgroundColor = [UIColor whiteColor];
    [sosuobgView.layer setMasksToBounds:YES];//编辑
    [sosuobgView.layer setCornerRadius:4.0];//圆角度
    [NVView addSubview:sosuobgView];
    //搜索按钮
    UIButton *SSbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    SSbutton.frame = CGRectMake(4, 2.5, 25, 25);
    [SSbutton setImage:[UIImage imageNamed:@"Imported Layers"] forState:UIControlStateNormal];
    [sosuobgView addSubview:SSbutton];
    //搜索输入框
    _SSSRTextField = [[UITextField alloc]initWithFrame:CGRectMake(30, 1, 461, 29)];
    _SSSRTextField.placeholder = @"请输入任务或商品名称";
    _SSSRTextField.font = [UIFont systemFontOfSize:13];
    [_SSSRTextField setValue:COLOR(187, 187, 187, 1) forKeyPath:@"_placeholderLabel.textColor"];
    _SSSRTextField.returnKeyType = UIReturnKeySearch;
    _SSSRTextField.delegate = self;
    [sosuobgView addSubview:_SSSRTextField];
}
//添加表视图
-(void)addShoppingMallTableView{
    //表格
    _ShoppingBigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 200)];
    _ShoppingBigView.backgroundColor = COLOR(229, 230, 231, 1);
    _ShoppingTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, width_screen, height_screen-64) style:UITableViewStyleGrouped];
    _ShoppingTabelView.backgroundColor = [UIColor clearColor];
    _ShoppingTabelView.tableHeaderView = _ShoppingBigView;
    _ShoppingTabelView.showsVerticalScrollIndicator = NO;//隐藏
    _ShoppingTabelView.separatorColor = COLOR(229, 230, 231, 1);//分割线颜色
    _ShoppingTabelView.delegate = self;
    _ShoppingTabelView.dataSource = self;
    [_ShoppingTabelView registerNib:[UINib nibWithNibName:@"LeeShoppingTableViewCell" bundle:nil] forCellReuseIdentifier:@"OenCell"];
    [self.view addSubview:_ShoppingTabelView];
    [self addShoppingTabelVieheardView];//加载表头
}
- (void)carousel:(PSCarouselView *)carousel didMoveToPage:(NSUInteger)page
{
    NSLog(@"PSCarouselView MOVE to No.%ld page",page);
    self.pageControl.currentPage = page;
}

-(void)scrollHttp
{
    [HTTPURL postRequest:LINK_BASE_URL(@"/goods/getBannerByType.api") parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary*dic=[PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        imgArr=(NSArray*)dic;
        NSLog(@"%@",imgArr);
        NSMutableArray*arr=[NSMutableArray new];
        [imgArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString*url=[NSString stringWithFormat:LINK_BASE_URL(@"%@"),obj[@"fileURL"]];
            [arr addObject:[NSURL URLWithString:url]];
        }];
        
        showView.imageURLs =arr;
//        showView.autoMoving = YES;
        [showView startMoving];

        self.pageControl.numberOfPages = imgArr.count;
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}
#pragma mark --表头View轮播图
-(void)addShoppingTabelVieheardView{
    showView = [[PSCarouselView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 195)];
    showView.imageViewMode=UIViewContentModeScaleAspectFill;
    showView.pageDelegate=self;
    showView.autoMoving = YES;
    showView.movingTimeInterval = 1.5f;
    [_ShoppingBigView addSubview:showView];
    
    _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(width_screen/2+50, 145, width_screen/2-50, 50)];
    [_ShoppingBigView addSubview:_pageControl];

}
//滑动视图点击方法
- (void)carousel:(PSCarouselView *)carousel didTouchPage:(NSUInteger)page
{
    NSLog(@"PSCarouselView did TOUCH No.%ld page",page);
}

#pragma mark --tableView 代理方法
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}
//区头视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *bigview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 40)];
    bigview.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 35)];
    label.text = _sectionTitleArr[0][section];
    label.textColor = COLOR(62,149, 208, 1);
    label.font = [UIFont systemFontOfSize:18];
    [bigview addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(width_screen-90, 0, 100, 35);
    [button setTitle:_sectionTitleArr[1][section] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.tag = 5+section;
    [button setTitleColor:COLOR(62, 149, 208, 1) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chankangenduoBtnClicke:) forControlEvents:UIControlEventTouchUpInside];
    [bigview addSubview:button];
    
    
    UIView *huiView = [[UIView alloc]initWithFrame:CGRectMake(0, 35, width_screen, 5)];
    huiView.backgroundColor = COLOR(235, 240, 243, 1);
    [bigview addSubview:huiView];
    return bigview;
}
//多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==1) {
        return 4;
    }else{
        
        return 0;
    }
}
//几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (section==1) {
        return 100;
    }else{
        return 495;
    }
}
//区尾
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section==1) {
        UIView *bigview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 60)];
        bigview.backgroundColor = COLOR(235, 240, 243, 1);
        return bigview;
    }else{
        UIView *bigview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen,535)];
        bigview.backgroundColor = COLOR(235, 240, 243, 1);
        
        for (int i=0; i<_shoppingArray.count; i++) {
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0+(width_screen/2+1)*(i%2), 0+(242)*(i/2), width_screen/2-1, 240)];
            view.backgroundColor = [UIColor whiteColor];
            [bigview addSubview:view];
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 15, width_screen/2-41, width_screen/2-41)];
            imgView.backgroundColor = [UIColor yellowColor];
            NSURL*url=[NSURL URLWithString:LINK_BASE_URL(_shoppingArray[i][@"fileName"])];
            [imgView setImageWithURL:url];
            [view addSubview:imgView];
            //标题
            UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 24+(width_screen/2-41), (width_screen/2-41), 25)];
            titleLabel.text = [NSString stringWithFormat:@"%@",_shoppingArray[i][@"goodName"]];
            //            titleLabel.backgroundColor = [UIColor redColor];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            [view addSubview:titleLabel];
            //金豆
            UILabel *JDLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, titleLabel.frame.origin.y+30, (width_screen/2-41)/2, 20)];
            JDLabel.textColor = COLOR(238, 88, 0, 1);
            JDLabel.text = [NSString stringWithFormat:@"%@ 金豆",_shoppingArray[i][@"ingot"]];
            JDLabel.textAlignment = NSTextAlignmentCenter;
            JDLabel.font = [UIFont systemFontOfSize:15];
            [view addSubview:JDLabel];
            //金币
            UILabel *JBLabel = [[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width/2, titleLabel.frame.origin.y+30, (width_screen/2-41)/2, 20)];
            JBLabel.textColor = COLOR(238, 88, 0, 1);
            JBLabel.text = [NSString stringWithFormat:@"%@ 金币",_shoppingArray[i][@"gold"]];
            JBLabel.textAlignment = NSTextAlignmentCenter;
            JBLabel.font = [UIFont systemFontOfSize:15];
            [view addSubview:JBLabel];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 0, width_screen/2-1, 240);
            [button addTarget:self action:@selector(mingPingBtnClciked:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 10+i;
            [view addSubview:button];
        }
        
        return bigview;
    }
    
    
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeeShoppingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OenCell"];
    if (_likeshoppingArray.count>indexPath.row) {
        
        cell.soldLabel.text = [NSString stringWithFormat:@"已售 %@",_likeshoppingArray[indexPath.row][@"count"]];
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",_likeshoppingArray[indexPath.row][@"goodName"]];
        cell.YBLabel.text = [NSString stringWithFormat:@"%@ 金豆",_likeshoppingArray[indexPath.row][@"ingot"]];
        cell.JBLabel.text = [NSString stringWithFormat:@"%@ 金币",_likeshoppingArray[indexPath.row][@"gold"]];
        NSURL*url=[NSURL URLWithString:LINK_BASE_URL(_likeshoppingArray[indexPath.row][@"fileName"])];
        [cell.ImgView setImageWithURL:url];
    }
    return cell;
    
}

#pragma mark---按钮点击事件
-(void)mingPingBtnClciked:(UIButton *)button{
    NSLog(@"%ld",(long)button.tag);
    CommodityDetailsViewController*vc=[CommodityDetailsViewController new];
    
        NSString*idStr=[NSString stringWithFormat:@"%ld",[_shoppingArray[button.tag-10][@"id"]integerValue]] ;
        vc.ID=idStr;
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CommodityDetailsViewController*vc=[CommodityDetailsViewController new];
    if (_likeshoppingArray.count>indexPath.row) {
        
        NSString*idStr=[NSString stringWithFormat:@"%ld",[_likeshoppingArray[indexPath.row][@"id"]integerValue]] ;
        vc.ID=idStr;
    }

    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
}



//cell 分割线 两端封头 实现这个两个方法 1
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark--查看跟多按钮 换一批事件
-(void)chankangenduoBtnClicke:(UIButton *)button{
    if (button.tag==5) {
        [self.navigationController pushViewController:[LeeMingPingGengDuoViewController new] animated:YES];
    }else{
        NSLog(@"换一批");
        [self httpRquestURLshoppinglike];
    }
}

#pragma mark--键盘搜索事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"点击了搜索");
    [self.view endEditing:YES];
    LeeGoodsListViewController *vc = [LeeGoodsListViewController new];
    vc.shoppingName = textField.text;
    
    vc.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
    return YES;
}
#pragma mark--页面将要进入前台
//页面将要进入前台，开启定时器

//名品商品
-(void)httpRquestURLshopping{
    __weak typeof(self) weakSelf = self;
    _shoppingArray = [NSArray array];
    
    [HTTPURL postRequest:LINK_BASE_URL(@"goods/homeGoods.api") parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        _shoppingArray = (NSArray *)dic;
        NSLog(@"%@",_shoppingArray);
        [weakSelf httpRquestURLshoppinglike];//猜你喜欢
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
    
    
    
}

//猜你喜欢
-(void)httpRquestURLshoppinglike{
    _likeshoppingArray = [NSArray array];
    NSLog(@"%@",userid);
    [HTTPURL postRequest:LINK_BASE_URL(@"goods/guessYouLike.api") parameters:@{@"userId":userid} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        if (![responseObject[@"data"]isEqual:[NSNull null]]) {
            NSDictionary *dic = [PublicTools dictionaryWithJsonString:responseObject[@"data"]];
            _likeshoppingArray = (NSArray *)dic;
            NSLog(@"%@",_likeshoppingArray);
            [_ShoppingTabelView reloadData];
            
        }
        
    } filure:^(NSURLSessionDataTask *task, id error) {
        NSLog(@"%@",error);
    }];
    
    
    
}

@end
